package com.hsy.baoinbao.utils;

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.deparse.dpframe.utils.DPHttpUtil.HttpCallback;
import com.deparse.dpframe.utils.DPLogUtil;
import com.hsy.baoinbao.R;
import com.hsy.baoinbao.common.Consts;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Map;

/**
 * Created by hsy on 2016/3/23.
 */
public class HttpUtil {
    private static Context applicationContext;

    public static void init(Context applicationContext) {
        DPHttpUtil.init(applicationContext);
        HttpUtil.applicationContext = applicationContext;
    }

    private static void handleSuccess(int what, String response, final HttpCallback httpCallback) {
        if (null == httpCallback) {
            return;
        }
        try {
            JSONObject jsonObject = new JSONObject(response);
            if (jsonObject.has(Consts.ERROR) && jsonObject.has(Consts.MSG)) {
                if (0 == jsonObject.getInt(Consts.ERROR)) {
                    httpCallback.onSuccess(what, jsonObject.getString(Consts.RESULT));
                } else {
                    CommonUtils.showToast(applicationContext, jsonObject.getString(Consts.MSG));
                    httpCallback.onError(what, Consts.TYPE_CODE_ERROR, null);
                }
            } else {
                CommonUtils.showToast(applicationContext, R.string.txt_network_failure);
                DPLogUtil.e("handleSuccess. Network failure 1 , response=" + response);
                httpCallback.onError(what, Consts.TYPE_NETWORK_ERROR, null);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            CommonUtils.showToast(applicationContext, R.string.txt_network_failure);
            DPLogUtil.e("handleSuccess. Network failure 2 , response=" + response + ", " + e.getMessage());
            httpCallback.onError(what, Consts.TYPE_NETWORK_ERROR, null);
        }
    }

    private static void handleError(int what, int type, VolleyError error, final HttpCallback httpCallback) {
        if (null == httpCallback) {
            return;
        }
        if (null == error || null == error.networkResponse) {
            CommonUtils.showToast(applicationContext, R.string.txt_network_failure);
            httpCallback.onError(what, Consts.TYPE_NETWORK_ERROR, error);
        } else {
            int statusCode = error.networkResponse.statusCode;
            int msg;
            switch (statusCode) {
                case 400:
                    msg = R.string.txt_param_lacked;
                    break;
                case 401:
                    msg = R.string.txt_auth_falure;
                    break;
                case 404:
                    msg = R.string.txt_invalid_request;
                    break;
                case 405:
                    msg = R.string.txt_param_send_on_a_wrong_way;
                    break;
                case 500:
                    msg = R.string.txt_internal_server_error;
                    break;
                default:
                    msg = R.string.txt_network_failure;
                    break;
            }

            DPLogUtil.e("handleError " + applicationContext.getString(msg));

            String data = null;
            try {
                data = new String(error.networkResponse.data, "UTF-8");
                DPLogUtil.d("data==" + data);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                DPLogUtil.d("e.getMessage()==" + e.getMessage());
            }
            try {
                JSONObject jsonObject = new JSONObject(data);
                if (jsonObject.has(Consts.ERROR) && jsonObject.has(Consts.MSG)) {
                    CommonUtils.showToast(applicationContext, jsonObject.getString(Consts.MSG));
                    httpCallback.onError(what, Consts.TYPE_CODE_ERROR, null);
                } else {
                    CommonUtils.showToast(applicationContext, R.string.txt_network_failure);
                    httpCallback.onError(what, Consts.TYPE_NETWORK_ERROR, null);
                }
            } catch (JSONException e) {
                e.printStackTrace();
                httpCallback.onError(what, Consts.TYPE_NETWORK_ERROR, error);
            }
        }
    }

    public static void httpGet(Context activityContext, final int what, final String url, final HttpCallback httpCallback) {
        if (null == applicationContext) {
            DPLogUtil.e("applicationContext = null");
            return;
        }
        DPHttpUtil.httpGet(what, url, new HttpCallback() {
            @Override
            public void onSuccess(int what, String response) {
                DPLogUtil.d("onSuccess what=" + what + ", response=" + response);
                handleSuccess(what, response, httpCallback);
            }

            @Override
            public void onError(int what, int type, VolleyError error) {
                DPLogUtil.e("onError what=" + what + ", type=" + type + ", error=" + (null == error ? "null" : error.toString()));
                handleError(what, type, error, httpCallback);
            }
        });
    }

    public static void httpPostWithParams(Context activityContext, final int what, final String url, final Map<String, String> params, final HttpCallback httpCallback) {
        if (null == applicationContext) {
            DPLogUtil.e("applicationContext = null");
            return;
        }
        DPHttpUtil.httpPostWithParams(what, url, params, new HttpCallback() {
            @Override
            public void onSuccess(int what, String response) {
                DPLogUtil.d("onSuccess what=" + what + ", response=" + response);
                handleSuccess(what, response, httpCallback);
            }

            @Override
            public void onError(int what, int type, VolleyError error) {
                handleError(what, type, error, httpCallback);
            }
        });
    }
}
